<?php
include('class/class_connect.php');
include('class/class_page.php');
include('class/class_address.php');
include('class/class_workdays.php');
include('class/class_rating.php');
include('class/abstract/class_mainObjectInfo.php');
include('class/city_rus.php');
include('class/class_checkGetData.php');
include('includes/transliterate.php');

$aCheck=new checkGetData('m');
$aCheck->checkTableId($_GET['city'], $_GET['id']);
foreach($_GET as $key=>$vol)
	$_GET[$key]=$aCheck->safetyCheck($vol);
foreach($_POST as $key=>$vol)
	$_POST[$key]=$aCheck->safetyCheck($vol);
session_start();
class aFirmPage extends page
{
	var $row=array();
	var $cityRus;
	function __construct()
	{
		$aConnect=new connect('m');
		$this->row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
		$this->cityRus=new city_rus_class();
		parent::__construct();
	}
	function title()
	{
		echo $this->row['name'].' '.$this->cityRus->city_rus_im($_GET['city']);
	}
	function maincontent()
	{
		echo ('
		<link rel="stylesheet" type="text/css" href="style/malls.css" />
		<div class="photoFrame">
		</div>
		<script type="text/javascript" src="scripts/mallsSearchSuggest.js">
		</script>
		<div class="route">
			<div class="routeEl">
				<a href="malls_list.php?city='.$_GET['city'].'">Торговые центры</a>
			</div>
			<div class="routeEl">
				<a href="malls.php?city='.$_GET['city'].'&id='.$_GET['id'].'">'.$this->row['name'].'</a>
			</div>
			<div class="routeEl">
				Фирмы
			</div>
		</div>
		');
		$aFirmInfo=new aFirmInfo($this->row);
		$aConnect=new connect('m');
		$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
		$mallsFirmsArray=explode('|', $row['firms']);
		for($i=0; $i<count($mallsFirmsArray); $i++)
		{
			$aMallsFirm=$mallsFirmsArray[$i];
			$aMallsFirmEl=explode('^', $aMallsFirm);
			$aMallsFirmId=$aMallsFirmEl[0];
			$aMallsFirmName=$aMallsFirmEl[1];
			$aMallsFirmSubdiv=$aMallsFirmEl[2];
			$aFirm[$aMallsFirmSubdiv][]=$aMallsFirmId.','.$aMallsFirmName;

		}
		arsort($aFirm);
		if($_GET['mallsSearch'] && !$_POST['mallsSearch'])$value=$_GET['mallsSearch'];
			else $value=$_POST['mallsSearch'];
		echo ('
		<input id="mallsId" name="mallsId" type="hidden" value="'.$_GET['id'].'">
		<center>
		<h3 id="formHeader">Наши фирмы</h3>
		<div id="mallsSearch">
			<form name="" id="mallsSearchForm" action="mallsFirms.php?city='.$_GET['city'].'&id='.$_GET['id'].'" method="post">
	        	<input id="mallsSearchInput" name="mallsSearch" type="text" value="" autocomplete="off">
	        	<input type="submit" value="Поиск" name="mallsSearchButton" class="searchButton">
			</form>
			<div id="mallsSearchSuggest">
			</div>
		</div>
		</center>
		');
		if(!$_POST['mallsSearch'] && !$_GET['mallsSearch'])
		{
			foreach($aFirm as $Firm=>$subdiv)
			{
				echo ('
				<div class="mallsSubdiv" align="center">
					<a href="">'.$Firm.' ('.count($subdiv).')</a>
				</div>
				<div class="mallsFirmsList">
				');
				for($i=0; $i<count($aFirm); $i++)
				{
					$aFirmsId=$subdiv[$i];
					$firmsIdNameArray=explode(',', $subdiv[$i]);
					$aFirmsId=$firmsIdNameArray[0];
					$aFirmsName=$firmsIdNameArray[1];
					echo ('

						<a href="firms.php?city='.$_GET['city'].'&id='.$aFirmsId.'">
							<div class="mallsFirms">
						');
						if(file_exists('pictures/firmspics/'.$_GET['city'].'/'.$aFirmsId.'.jpg'))
							echo '<img class="mallsPics" src="pictures/firmspics/'.$_GET['city'].'/'.$aFirmsId.'.jpg" title="'.$aFirmsName.'" />';
						echo ('
							</div>
						</a>

					');
					//echo $aFirmsId.$aFirmsName;
				}
				echo ('
				</div>
				');
				//echo count($subdiv);
				//echo $subdiv;
				//echo $aFirm;
	    	//$aTab=new aFirmTabs();
	    	}
	  	}
	  	else
	  	{
	  		if($_GET['mallsSearch'] && !$_POST['mallsSearch'])$_POST['mallsSearch']=$_GET['mallsSearch'];
	  		$aConnect=new connect('f');
	  		$res=$aConnect->select_all($_GET['city'], "WHERE (name LIKE '%".$_POST['mallsSearch']."%'
	  		 || subdiv LIKE '%".$_POST['mallsSearch']."%' || short LIKE '%".$_POST['mallsSearch']."%')
	  		 AND address LIKE '%^".$row['id'].",".$row['name'].",".$row['short']."^%'
	  		 ORDER BY rating DESC");
	  		 $rating=new rating('f');
	  		if(!mysql_num_rows($res))
	  		{
	  			$word=mb_transliterate($_POST['mallsSearch']);
	  			$res=$aConnect->select_all($_GET['city'], "WHERE (name LIKE '%".$word."%'
	  			 || subdiv LIKE '%".$word."%' || short LIKE '%".$word."%')
	  		 	AND address LIKE '%^".$row['id'].",".$row['name'].",".$row['short']."^%'
	  		 	ORDER BY rating DESC");
	  		}
	  		while($row1=mysql_fetch_array($res))
	  		{
	  			echo ('
	  			<div class="mallsFirm">
	  				<table border="0">
	  					<tr>
	  						<td width="300">
	  							<div class="mallsFirmLogo">
	  					        	<a href="firms.php?city='.$_GET['city'].'&id='.$row1['id'].'">
	  					        		<img src="pictures/firmspics/'.$_GET['city'].'/'.$row1['id'].'.jpg" />
	  								</a>
	  							</div>
	  						</td>
	  						<td>
	  							<div class="mallsFirmsName">
	  								<a href="firms.php?city='.$_GET['city'].'&id='.$row1['id'].'">'.$row1['name'].'</a>
	  							</div>
	  							<div class="mallsFirmsShort">
	  								'.$row1['short'].'
	  							</div>
	  							<div class="mallsFirmsRating">
	  			');
	  								$rating->echo_rating(0, $row1['rating'], $row1['votes']);
	  	        echo ('
	  							</div>
	  						</td>
	  					</tr>
	  				</table>
	  			</div>
	  			');
	  		}
		}
	}
}



class aFirmInfo extends mainObjectInfo
{
	var $row;
	function __construct($row=array())
	{
		$this->row=$row;
		parent::__construct();
	}
	function showPic()
	{
		if(file_exists('pictures/mallspics/'.$_GET['city'].'/'.$this->row['id'].'.jpg'))
		echo ('
		<a href="malls.php?city='.$_GET['city'].'&id='.$_GET['id'].'">
			<img src="pictures/mallspics/'.$_GET['city'].'/'.$this->row['id'].'.jpg" title="'.$this->name.'">
		</a>
		');
		$aFirmRating=new rating('m');
		$aFirmRating->rating($_GET['city'], $this->row['id']);
	}
	function showAddress()
	{
		$aWorkdays=new workdays();
		$address=new address();
		$explodedAddress=$this->row['address'].'^^'.$this->row['phone'].'^^'.$this->row['workdays'];
		$address->showAddress($explodedAddress, $this->row['link']);
	}
	function showshort()
	{
		echo $this->row['short'];
	}
	function showName()
	{
		echo $this->row['name'];
	}
}

$aPage=new aFirmPage();

?>